package com.binance.notification.web.service.abnormaltrade.integration;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.binance.assetservice.vo.response.product.FetchProductsResponse;
import com.binance.notification.web.service.abnormaltrade.log.LoggerUtils;
import com.binance.notification.web.service.helper.ProductService;
import com.google.common.collect.Lists;

/**
 * 对接exchange的币种基本信息接口
 */
@Service
public class ExchangeProductService {
    private static final org.slf4j.Logger log = LoggerUtils.getLogger(ExchangeProductService.class);
    @Autowired
    private ProductService productApi;

    private List<FetchProductsResponse> getAllProducts() throws Exception {
        return this.productApi.getAllProducts();
    }

    /**
     * 获取以以某个币计价的交易对，例如btcusdt，ethusdt,bnbbtc
     * 
     * @throws Exception
     */
    public List<String> getSymbolListForDefinedCoin(String symbol) throws Exception {
        log.info("ExchangeProductService.getSymbolListForDefinedCoin symbol={} ", symbol);
        List<FetchProductsResponse> exchangeProductInfos = getAllProducts();
        log.info("ExchangeProductService.getSymbolListForDefinedCoin exchangeProductInfos={} ", exchangeProductInfos);
        List<String> symbolList = Lists.newArrayList();
        for (FetchProductsResponse exchangeProductInfo : exchangeProductInfos) {
            log.info("ExchangeProductService.getSymbolListForDefinedCoin exchangeProductInfo={} ", exchangeProductInfo);
            if (symbol.equalsIgnoreCase(String.valueOf(exchangeProductInfo.getQuoteAsset()))) {
                log.info("ExchangeProductService.getSymbolListForDefinedCoin add symbol={} ",
                        String.valueOf(exchangeProductInfo.getSymbol()));
                symbolList.add(String.valueOf(exchangeProductInfo.getSymbol()));
            }
        }
        return symbolList;
    }

    /**
     * 获取以BTC计价的交易对，例如bnbbtc
     * 
     * @throws Exception
     */
    public List<String> getBtcSymbolList() throws Exception {
        List<String> symbolList = getSymbolListForDefinedCoin("BTC");
        return symbolList;
    }

    /**
     * 获取以USDT计价的交易对，例如bnbusdt
     * 
     * @throws Exception
     */
    public List<String> getUsdtSymbolList() throws Exception {
        List<String> symbolList = getSymbolListForDefinedCoin("USDT");
        return symbolList;
    }

    /**
     * 获取以usdt和btc计价的交易对
     * 
     * @throws Exception
     */
    public List<String> getUsdtAndBtcSymbolList() throws Exception {
        List<String> symbolList = Lists.newArrayList();
        List<String> usdtSymbolList = getSymbolListForDefinedCoin("USDT");
        List<String> btcSymbolList = getSymbolListForDefinedCoin("BTC");
        symbolList.addAll(usdtSymbolList);
        symbolList.addAll(btcSymbolList);
        return symbolList;
    }

}
